interface ApiResponse<T = any> {
  success: boolean;
  data: T;
  message: string;
}

interface WeatherParams {
  city?: string;
  location?: string;
}

interface GeocodeParams {
  address?: string;
  location?: string;
  type?: 'geo' | 'regeo';
}

interface PoiParams {
  location: string;
  keywords?: string;
  radius?: number;
  types?: string;
}

interface RouteParams {
  origin: string;
  destination: string;
  strategy?: number;
  waypoints?: string;
}

// 后端服务器配置
const BACKEND_BASE_URL = 'http://localhost:3002';

export const useBackendApi = () => {
  // 天气API
  const getWeather = async (params: WeatherParams): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    if (params.city) query.append('city', params.city);
    if (params.location) query.append('location', params.location);
    
    return await $fetch(`${BACKEND_BASE_URL}/api/weather?${query.toString()}`);
  };

  const getWeatherForecast = async (params: { city?: string; days?: number }): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    if (params.city) query.append('city', params.city);
    if (params.days) query.append('days', params.days.toString());
    
    return await $fetch(`${BACKEND_BASE_URL}/api/weather/forecast?${query.toString()}`);
  };

  const getCurrentWeather = async (params: { location?: string }): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    if (params.location) query.append('location', params.location);
    
    return await $fetch(`${BACKEND_BASE_URL}/api/weather/current?${query.toString()}`);
  };

  // 地理编码API
  const getGeocode = async (params: GeocodeParams): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    if (params.address) query.append('address', params.address);
    if (params.location) query.append('location', params.location);
    if (params.type) query.append('type', params.type);
    
    return await $fetch(`${BACKEND_BASE_URL}/api/geocode?${query.toString()}`);
  };

  const getRegeocode = async (params: { location: string }): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    query.append('location', params.location);
    
    return await $fetch(`${BACKEND_BASE_URL}/api/geocode/regeo?${query.toString()}`);
  };

  const getGeo = async (params: { address: string }): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    query.append('address', params.address);
    
    return await $fetch(`${BACKEND_BASE_URL}/api/geocode/geo?${query.toString()}`);
  };

  // POI搜索API
  const getPoi = async (params: PoiParams): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    query.append('location', params.location);
    if (params.keywords) query.append('keywords', params.keywords);
    if (params.radius) query.append('radius', params.radius.toString());
    if (params.types) query.append('types', params.types);
    
    return await $fetch(`${BACKEND_BASE_URL}/api/poi?${query.toString()}`);
  };

  const getNearbyPoi = async (params: { location: string; radius?: number }): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    query.append('location', params.location);
    if (params.radius) query.append('radius', params.radius.toString());
    
    return await $fetch(`${BACKEND_BASE_URL}/api/poi/nearby?${query.toString()}`);
  };

  const getPoiDetail = async (id: string): Promise<ApiResponse> => {
    return await $fetch(`${BACKEND_BASE_URL}/api/poi/detail/${id}`);
  };

  // 路径规划API
  const getRoute = async (params: RouteParams): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    query.append('origin', params.origin);
    query.append('destination', params.destination);
    if (params.strategy) query.append('strategy', params.strategy.toString());
    if (params.waypoints) query.append('waypoints', params.waypoints);
    
    return await $fetch(`${BACKEND_BASE_URL}/api/route?${query.toString()}`);
  };

  const getDrivingRoute = async (params: { origin: string; destination: string; strategy?: number }): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    query.append('origin', params.origin);
    query.append('destination', params.destination);
    if (params.strategy) query.append('strategy', params.strategy.toString());
    
    return await $fetch(`${BACKEND_BASE_URL}/api/route/driving?${query.toString()}`);
  };

  const getWalkingRoute = async (params: { origin: string; destination: string }): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    query.append('origin', params.origin);
    query.append('destination', params.destination);
    
    return await $fetch(`${BACKEND_BASE_URL}/api/route/walking?${query.toString()}`);
  };

  const getTransitRoute = async (params: { origin: string; destination: string; date?: string }): Promise<ApiResponse> => {
    const query = new URLSearchParams();
    query.append('origin', params.origin);
    query.append('destination', params.destination);
    if (params.date) query.append('date', params.date);
    
    return await $fetch(`${BACKEND_BASE_URL}/api/route/transit?${query.toString()}`);
  };

  // 健康检查
  const getHealth = async (): Promise<ApiResponse> => {
    return await $fetch(`${BACKEND_BASE_URL}/health`);
  };

  // 服务器信息
  const getServerInfo = async (): Promise<ApiResponse> => {
    return await $fetch(`${BACKEND_BASE_URL}/`);
  };

  return {
    // 天气API
    getWeather,
    getWeatherForecast,
    getCurrentWeather,
    
    // 地理编码API
    getGeocode,
    getRegeocode,
    getGeo,
    
    // POI搜索API
    getPoi,
    getNearbyPoi,
    getPoiDetail,
    
    // 路径规划API
    getRoute,
    getDrivingRoute,
    getWalkingRoute,
    getTransitRoute,
    
    // 系统API
    getHealth,
    getServerInfo
  };
}; 